import { ref } from "vue";

const iframeRef: any = ref();
//导出iframeRef引用，以便在其他组件中使用
export const useIframeRef = () => {
    return iframeRef
};
/**
 * 播放的方法
 */
export const useHandlePlay = () => {
    if (iframeRef.value) {
        let datas = {
            type: "play",
            data: true
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}

/**
 *  重置方法
 */
export const useHandleReset = () => {
    if (iframeRef.value) {
        let datas = {
            type: "reset",
            data: true
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 外壳透明度设置方法
 * @param nums 透明度数值，介于0到100之间
 */
export const useHandleTransparent = (nums: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "transparent",
            data: nums
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}

/**
 * 动画演示开关
 * @param boolean 动画开关，true为开，false为关
 */
export const useHandleAnimation = (boolean: boolean) => {
    if (iframeRef.value) {
        let datas = {
            type: "animation",
            data: boolean
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 光学特效开关
 * @param boolean 光学开关，true为开，false为关
 */
export const useHandleOptical = (boolean: boolean) => {
    if (iframeRef.value) {
        let datas = {
            type: "optical",
            data: boolean
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}

/**
 * 组件选择
 * @param arr 组件字符串如'0,1,2,3'，0:光栅;1:反射镜;2:狭缝;3:探测器;
 */
export const useHandleComponent = (arr: string) => {
    if (iframeRef.value) {
        let datas = {
            type: "component",
            data: arr
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 控制波长
 * @param number 波长数值，介于0到1000之间nm
 */
export const useHandleWavelength = (number: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "wavelength",
            data: number
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 狭缝宽度
 * @param number 狭缝宽度数值，介于0到1之间mm
 */
export const useHandleSlitwidth = (number: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "slitwidth",
            data: number
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 光栅角度
 * @param number 波光栅角度数值，介于0到360之间°
 */
export const useHandleGratingangle = (number: number) => {
    if (iframeRef.value) {
        let datas = {
            type: "gratingangle",
            data: number
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 开始测量的方法
 */
export const useHandleStartMeasurement = () => {
    if (iframeRef.value) {
        let datas = {
            type: "startMeasurement",
            data: true
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 重置参数的方法
 */
export const useHandleResetParameters = () => {
    if (iframeRef.value) {
        let datas = {
            type: "resetParameters",
            data: true
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 保存配置的方法
 */
export const useHandleSaveConfiguration = () => {
    if (iframeRef.value) {
        let datas = {
            type: "saveConfiguration",
            data: true
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}
/**
 * 峰值分析的方法
 */
export const useHandlePeakAnalysis = () => {
    if (iframeRef.value) {
        let datas = {
            type: "peakAnalysis",
            data: true
        }
        iframeRef.value.contentWindow.postMessage(datas, "*");
    }
}



